package com.yang.dao;

import com.yang.entity.OrderDetail;

import java.sql.*;
import java.util.*;

public class OrderDetailDao {
    //查询全部
    public List<OrderDetail> queryOrderDetail() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from OrderDetail");
            rs = ps.executeQuery();
            List<OrderDetail> list = new ArrayList<>();
            while (rs.next()) {
                OrderDetail obj = new OrderDetail();
                obj.setId(rs.getInt("id"));
                obj.setProductName(rs.getString("productName"));
                obj.setProductPrice(rs.getObject("productPrice"));
                obj.setProductCount(rs.getInt("productCount"));
                obj.setOrderId(rs.getString("orderId"));
                obj.setProductId(rs.getInt("productId"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public OrderDetail queryOrderDetailById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from OrderDetail where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                OrderDetail obj = new OrderDetail();
                obj.setId(rs.getInt("id"));
                obj.setProductName(rs.getString("productName"));
                obj.setProductPrice(rs.getObject("productPrice"));
                obj.setProductCount(rs.getInt("productCount"));
                obj.setOrderId(rs.getString("orderId"));
                obj.setProductId(rs.getInt("productId"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveOrderDetail(OrderDetail obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into order_detail(product_name, product_price, product_count, order_id, product_id) values (?, ?, ?, ?, ?)");
            ps.setString(1, obj.getProductName());
            ps.setObject(2, obj.getProductPrice());
            ps.setInt(3, obj.getProductCount());
            ps.setString(4, obj.getOrderId());
            ps.setInt(5, obj.getProductId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteOrderDetailById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from OrderDetail where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateOrderDetailById(OrderDetail obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update order_detail set product_name=?, product_price=?, product_count=?, order_id=?, product_id=? where id=?");
            ps.setString(1, obj.getProductName());
            ps.setObject(2, obj.getProductPrice());
            ps.setInt(3, obj.getProductCount());
            ps.setString(4, obj.getOrderId());
            ps.setInt(5, obj.getProductId());
            ps.setInt(6, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
